﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DnnDash.SimplePhoneApp.Common;
using DnnDash.SimplePhoneApp.ViewModel;

namespace DnnDash.SimplePhoneApp
{
    /* requires Windows Phone Silverlight Toolkit 
     * http://windowsphonegeek.com/tips/How-to-install-Windows-Phone-Toolkit-Aug-2011-via-NuGet
     * http://silverlight.codeplex.com/
     */
    public partial class MainPage : PhoneApplicationPage
    {
        private DashViewModel vm;
        private const string DATAKEY = "DashboardDataKey";

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            vm = new DashViewModel();
            vm.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(vm_PropertyChanged);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            vm.DialogHelper = new Model.DialogService();
            vm.NavigationHelper = new Model.Navigator(this.NavigationService);

            if (!StateUtilities.IsLaunching && this.State.ContainsKey(DATAKEY))
            {
                // Application started from Back Button
                vm = (DashViewModel)this.State[DATAKEY];

                // If we don't have any Dash Controls yet, force a refresh.
                if (vm.DashControls.Count == 0)
                {
                    vm.DashboardDataFromService();
                    SetFavoriteTab();
                }
            }
            else
            {
                // New instance or state not saved yet
                vm.DashboardDataFromService();
                //set favorite tab
                SetFavoriteTab();
            }

            this.DataContext = vm.DashControls;

            if (this.ApplicationBar != null)
                this.ApplicationBar.IsVisible = false; //hide application bar
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            if (this.State.ContainsKey(DATAKEY))
            {
                this.State[DATAKEY] = vm;
            }
            else
            {
                this.State.Add(DATAKEY, vm);
            }

            StateUtilities.IsLaunching = false;
        }
        
        private void OnAppBarRefreshClick(object sender, EventArgs e)
        {
            if (vm != null)
                vm.DashboardDataFromService();
        }

        private void OnAppBarSettingsClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/SettingsPage.xaml", UriKind.Relative));
        }

        private void OnAppBarAboutClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AboutPage.xaml", UriKind.Relative));
        }

        private void TapAndHold(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            if (this.ApplicationBar != null)
                this.ApplicationBar.IsVisible = !this.ApplicationBar.IsVisible;
        }

        private void Favorite_Click(object sender, RoutedEventArgs e)
        {
            var pivotItem = this.uiDashPivot.SelectedItem;
            if (pivotItem.GetType() == typeof(ObservableDashGroups))
            {
                ObservableDashGroups groups = (ObservableDashGroups)pivotItem;
                vm.SaveFavoriteControl(groups.GroupsName);
            }
        }
        private void vm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //a property changed
            switch (e.PropertyName)
            {
                case "FavoriteTabName":
                    SetFavoriteTab();
                    break;
            }
        }

        private void SetFavoriteTab()
        {
            if (vm.FavoriteSelectedIndex > -1 && vm.FavoriteSelectedIndex < this.uiDashPivot.Items.Count)
                this.uiDashPivot.SelectedIndex = vm.FavoriteSelectedIndex;

        }
        
    }
}